package com.ms3ds.mineInfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ms3ds.common.core.domain.R;
import com.ms3ds.common.security.utils.SecurityUtils;
import com.ms3ds.mineInfo.api.domain.Mine;
import com.ms3ds.mineInfo.api.domain.Team;
import com.ms3ds.mineInfo.service.IMineService;
import com.ms3ds.mineInfo.service.ITeamService;
import com.ms3ds.system.api.RemoteRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ms3ds.common.log.annotation.Log;
import com.ms3ds.common.log.enums.BusinessType;
import com.ms3ds.mineInfo.api.domain.Worker;
import com.ms3ds.mineInfo.service.IWorkerService;
import com.ms3ds.common.core.web.controller.BaseController;
import com.ms3ds.common.core.web.domain.AjaxResult;
import com.ms3ds.common.core.utils.poi.ExcelUtil;
import com.ms3ds.common.core.web.page.TableDataInfo;

/**
 * 班组Controller
 * 
 * @author zlw
 * @date 2025-09-18
 */
@RestController
@RequestMapping("/worker")
public class WorkerController extends BaseController
{
    @Autowired
    private IWorkerService workerService;

    @Autowired
    private RemoteRoleService remoteRoleService;

    @Autowired
    private IMineService mineService;

    @Autowired
    private ITeamService teamService;

    /**
     * 查询班组列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Worker worker)
    {
        startPage();
        List<Worker> list = workerService.selectWorkerList(worker);
        return getDataTable(list);
    }

    @GetMapping("/getSystemPreviewWorker")
    public AjaxResult getSystemPreviewWorker(Long mineId){
        return success(workerService.getSystemPreviewWorker(mineId));
    }

    @GetMapping("/getDecisionSupportWorker")
    public AjaxResult getDecisionSupportWorker(Long mineId){
        return success(workerService.getDecisionSupportWorker(mineId));
    }

    /**
     * 获取班组树形结构
     * @return
     */
    @GetMapping("/getTree")
    public AjaxResult getWorkerTree(){
        //先获取所有矿山信息
        Long userId = SecurityUtils.getUserId();
        R<Boolean> res = remoteRoleService.isAdmin(userId);
        List<Mine> mineList = null;
        if(res!=null&&res.getData()){//如果是管理员则查询所有数据
            mineList = mineService.selectMineList(new Mine());
        } else {
            mineList = mineService.selectMineListByUserId(new Mine(),userId);
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        //再遍历矿山获取对应的生产队信息
        for(Mine mine:mineList){
            Map<String, Object> map = new HashMap<>();
            map.put("label", mine.getMineName());
            map.put("value", mine.getId());
            map.put("level", "mine");
            //开始获取生产队
            List<Map<String, Object>> teamMapList = new ArrayList<>();
            Team searchTeam = new Team();
            searchTeam.setMineId(mine.getId());
            List<Team> teams = teamService.selectTeamList(searchTeam);
            for(Team team:teams){
                Map<String,Object> teamMap = new HashMap<>();
                teamMap.put("value", team.getId());
                teamMap.put("label",team.getTeamName());
                teamMap.put("level", "team");
                teamMap.put("mineId", team.getMineId());
                //开始获取工人数据
                List<Map<String, Object>> workerMapList = new ArrayList<>();
                Worker workerSerch = new Worker();
                workerSerch.setTeamId(team.getId());
                List<Worker> workers = workerService.selectWorkerList(workerSerch);
                for(Worker worker:workers){
                    Map<String,Object> workerMap = new HashMap<>();
                    workerMap.put("value", worker.getId());
                    workerMap.put("label", worker.getWorkerName());
                    workerMap.put("level", "worker");
                    workerMap.put("mineId", team.getMineId());
                    workerMap.put("teamId", worker.getTeamId());
                    workerMapList.add(workerMap);
                }
                teamMap.put("children", workerMapList);
                teamMapList.add(teamMap);
            }
            map.put("children", teamMapList);
            mapList.add(map);
        }
        return success(mapList);
    }

    @GetMapping("/getWorkerByTeamIdForRemote")
    R<List<Worker>> getWorkerByTeamIdForRemote(Long teamId){
        Worker worker = new Worker();
        worker.setTeamId(teamId);
        List<Worker> workers = workerService.selectWorkerList(worker);
        return R.ok(workers);
    }

    /**
     * 导出班组列表
     */
    @Log(title = "班组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Worker worker)
    {
        List<Worker> list = workerService.selectWorkerList(worker);
        ExcelUtil<Worker> util = new ExcelUtil<Worker>(Worker.class);
        util.exportExcel(response, list, "班组数据");
    }

    /**
     * 获取班组详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(workerService.selectWorkerById(id));
    }

    /**
     * 新增班组
     */
    @Log(title = "班组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Worker worker)
    {
        return toAjax(workerService.insertWorker(worker));
    }

    /**
     * 修改班组
     */
    @Log(title = "班组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Worker worker)
    {
        return toAjax(workerService.updateWorker(worker));
    }


    @PutMapping("/editWorkerForRemote")
    public R<Boolean> editWorkerForRemote(@RequestBody Worker worker)
    {
        return R.ok(workerService.updateWorker(worker)==1);
    }

    /**
     * 删除班组
     */
    @Log(title = "班组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(workerService.deleteWorkerByIds(ids));
    }
}
